package com.zegome.game.gomestory.main.scene.play.board;

import java.util.Random;
import java.util.Vector;

import com.paypal.android.sdk.bo;
import com.paypal.android.sdk.bp;

public class GetDataMap {
	
	public Board board;
	
	public GetDataMap(Board board)
	{
		this.board = board;
	}

	public void set(String map, int mapRow, int mapCol,int time, int numberPika,int stylePlay){
		if(board.screen.ctr.d.isLDPI && board.isInDraw == false && board.isInVS == false){
			int deltaCol=2, deltaRow=2;
			map = getMap(map,mapRow,mapCol,deltaRow,deltaCol);
			mapCol -= deltaCol;
			mapRow -= deltaRow;			
		}
		
		if(board.isInVS == true){
			int deltaCol=mapCol - 10, deltaRow=mapRow - 6;
			map = getMap(map,mapRow,mapCol,deltaRow,deltaCol);
			mapCol -= deltaCol;
			mapRow -= deltaRow;			
		}
		if(time>=240000){
			time = 180000;
		}
		
		if(board == null){
			board.screen.ctr.androidHelper.log("LOI NULL BOARD");			
		}
		
		if(board.img == null){
			board.screen.ctr.androidHelper.log("LOI NULL BOARD.IMG");	
		}
		
		if(board.img.pika == null){
			board.screen.ctr.androidHelper.log("LOI NULL BOARD.IMG.PIKA");	
		}
				
		if(numberPika>=board.img.pika.length){
			numberPika = board.img.pika.length;
		}
		
		//		
		board.dataMap.squareNor = 2+(new Random().nextInt(4));
		board.dataMap.squarePress = board.dataMap.squareNor;
		while(board.dataMap.squarePress == board.dataMap.squareNor){
			board.dataMap.squarePress = new Random().nextInt(9);
		}
		// Load data from map and init variable
		//board.dataMap.stylePlay = 8;
		//board.dataMap.stylePlay = 9;
		board.dataMap.stylePlay = (new Random()).nextInt(8);
		board.dataMap.infoTypePika.removeAllElements();
		board.dataMap.time = time;
		board.dataMap.mapRow = mapRow;
		board.dataMap.mapCol = mapCol;
		board.dataMap.total = 0;
		int hiPika = (int) (board.dataMap.hiMap/(mapRow));
		int wiPika = (int) (board.dataMap.wiMap/(mapCol));
		board.dataMap.xBegin = board.dataMap.xBeginDraw-wiPika;
		board.dataMap.yBegin = board.dataMap.yBeginDraw-hiPika;
		board.dataMap.hi1Pikachu = hiPika;
		board.dataMap.wi1Pikachu = wiPika;
		board.dataMap.wiEdgeVer = (float)((float)(wiPika)/(float)(8));
		board.dataMap.hiEdgeVerUnit = (float)((float)hiPika/(float)9);
		board.dataMap.wiEdgeHorUnit = (float)((float)wiPika/(float)9);
		
		if(board.dataMap.stylePlay == 8){
			board.screen.removeChild(board.layerMode);
			int deltaR=2, deltaC=2;
			String m = getMap( "010011001011" + "101111110111" + "011111111011"
			+ "011111111011" + "101111110111" + "010011001011" + "101111110111" + "010011001011", mapRow, mapCol, deltaR, deltaC);
			board.layerMode.setData(board.screen.ctr.a.currentChapter, m, mapRow-deltaR, mapCol-deltaC);
			board.screen.attachChild(board.layerMode);
		}
		
		Vector<Possition> possitionAble = new Vector<Possition>();
		Vector<Integer> useAblePika = new Vector<Integer>();
		int i,k,rc;
		Random randPika = new Random();
		// Test new icon 
		for(i=0;i<numberPika;i++){
			useAblePika.add(i);
		}		
		for(i=0;i<numberPika;i++){			
			k = new Random().nextInt(useAblePika.size());
			board.dataMap.infoTypePika.add(new InfoTypePika(useAblePika.elementAt(k), 0));
			useAblePika.removeElementAt(k);
		}
		
		// Second: Prepare map pikaItem from String map
		
		for(int row=0;row<10;row++){
			for(int col=0;col<14;col++){

				board.dataMap.map[row][col].setPosition(board.dataMap.xBegin+board.dataMap.wi1Pikachu*col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*row);
				board.dataMap.map[row][col].setSize(wiPika, hiPika);
				board.dataMap.map[row][col].set(-1);
				board.dataMap.map[row][col].setSelect(false);
				board.dataMap.map[row][col].isMove = false;				
				board.screen.removeChild(board.dataMap.map[row][col]);
			}
		}	
		
		int r,c;
		
		r=1;
		c=1;	
		
		for(i=0;i<map.length();i++){
			if(map.substring(i, i+1).equals("1")){
				possitionAble.add(new Possition(r,c));
				board.dataMap.total++;
			}
			else{
				board.dataMap.map[r][c].set(-1);
			}
			c++;
			if(c==board.dataMap.mapCol+1){
				c=1;
				r++;
			}
		}	
		
		if(possitionAble.size()%2==1){
			possitionAble.removeElementAt(0);
			board.dataMap.total -= 1;
		}
		
		board.dataMap.totalPikaFix = board.dataMap.total;
		// Third: Put in map
		while (possitionAble.size()!=0) {
			
			k = randPika.nextInt(board.dataMap.infoTypePika.size());
			rc = new Random().nextInt(possitionAble.size());
			
			board.dataMap.map[possitionAble.elementAt(rc).row][possitionAble.elementAt(rc).col].set(board.dataMap.infoTypePika.elementAt(k).type);
			possitionAble.removeElementAt(rc);
			
			rc = new Random().nextInt(possitionAble.size());
			board.dataMap.map[possitionAble.elementAt(rc).row][possitionAble.elementAt(rc).col].set(board.dataMap.infoTypePika.elementAt(k).type);
			possitionAble.removeElementAt(rc);
			
			board.dataMap.infoTypePika.elementAt(k).total+=2;
		}	
		
		if(board.bgPikachu!=null){
			board.screen.removeChild(board.bgPikachu);			
		}
		
		board.bgPikachu.getMap();
		board.screen.attachChild(board.bgPikachu);
		
		for(int row=0;row<10;row++){
			for(int col=0;col<14;col++){
				board.dataMap.map[row][col].setPosition(board.dataMap.xBegin+board.dataMap.wi1Pikachu*col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*row);			
				board.screen.attachChild(board.dataMap.map[row][col]);
			}
		}		
	}
		
	public void loadLevel(int level){
		switch (level) {
		case 1:
			set("0001111000" + "0001001000" + "0001001000"
					+ "0001001000" + "0001001000" + "0001111000", 6, 10, 90000, 4,3);
			break;
		case 2:
			set("1111111111" + "0001001000" + "0001001000"
					+ "0001001000" + "0001001000" + "1111111111", 6, 10, 90000, 8,2);
			break;
		case 3:
			set("0111001110" + "1000110001" + "1000110001"
					+ "1000110001" + "1000110001" + "0111001110", 6, 10, 90000, 8,3);
			break;
		case 4:
			set("0100110010" + "1010110101" + "0100110010"
					+ "0100110010" + "1010110101" + "0100110010", 6, 10, 90000, 10,4);
			break;
		case 5:
			set("0100110010" + "1011111101" + "0111111110"
					+ "0111111110" + "1011111101" + "0100110010", 6, 10, 180000, 14,4);
			break;
		case 6:
			set("0111111110" + "1101111011" + "1111111111"
					+ "1011111101" + "1100000011" + "0111111110", 6, 10, 180000, 18,4);
			break;
		case 7:
			set("1011111101" + "1101111011" + "1111111111"
					+ "1111111111" + "1101111011" + "1011111101", 6, 10, 180000, 18,4);
			break;
		case 8:
			set("0111111110" + "1111111111" + "1111001111"
					+ "1110110111" + "1111111111" + "0111111110", 6, 10, 180000, 18,4);
			break;
		case 9:
			set("1111111111" + "1111111111" + "1111111111"
					+ "1111111111" + "1111111111" + "1111111111", 6,10,180000, 20,0);
			break;
		case 10:
			set("1111111111" + "1111111111" + "1111111111"
					+ "1111111111" + "1111111111" + "1111111111", 6,10,180000, 20,1);
			break;
		case 11:
			set("1111111111" + "1111111111" + "1111111111"
					+ "1111111111" + "1111111111" + "1111111111", 6,10,180000, 20,2);
			break;
		case 12:
			set("1111111111" + "1111111111" + "1111111111"
					+ "1111111111" + "1111111111" + "1111111111", 6,10,180000, 20,3);
			break;
			
		case 13:
			set("01111111110" + "10000100001" + "11111111111"
					+ "11111111111" + "11111111111" + "11111111111",6,11, 180000, 14,4);
			break;
		case 14:
			set("10110101101" + "00111111100" + "00111111100"
					+ "10011111001" + "01001110010" + "00100100100",6,11, 180000,16,4);
			break;
		case 15:
			set("01011111011" + "11101110011" + "01001110000"
					+ "00001110010" + "11001110111" + "11011111010",6,11, 180000, 18,4);
			break;
		case 16:
			set("11111111111" + "11000000011" + "10111111101"
					+ "10111111101" + "11000000011" + "11111111111",6,11, 180000, 18,4);
			break;
		case 17:
			set("10000100001" + "10001110001" + "10011111001"
					+ "10111111101" + "11111111111" + "11111111111",6,11, 180000, 24,4);
			break;
		case 18:
			set("11111111111" + "11111111111" + "11111111111"
					+ "11111111111" + "11111111111" + "11111111111",6,11, 200000, 24,4);
			break;
		case 19:
			set("11111111111" + "11111111111" + "11111111111"
					+ "11111111111" + "11111111111" + "11111111111",6,11, 200000, 28,0);
			break;
		case 20:
			set("11111111111" + "11111111111" + "11111111111"
					+ "11111111111" + "11111111111" + "11111111111",6,11, 200000, 28,1);
			break;
		case 21:
			set("01111111111" + "01111111111" + "00111111111"
					+ "00011111111" + "00001111111" + "00000111111",6,11, 180000, 32,4);
			break;
		case 22:
			set("11111111111" + "11111111111" + "11111111111"
					+ "11111111111" + "11111111111" + "11111111111",6,11, 180000, 32,2);
			break;
		case 23:
			set("11111100000" + "11111110000" + "11111111000"
					+ "11111111100" + "11111111110" + "11111111110",6,11, 180000,  32,4);
			break;
		case 24:
			set("11111111111" + "11111111111" + "11111111111"
					+ "11111111111" + "11111111111" + "11111111111",6,11, 200000,  32,3);
			break;
		case 25:
			set("111111101010" + "101010101101"
					+ "101010101101" + "111010110111" + "100010101101"
					+ "100010101101" + "100111101101",7,12, 180000,  22,4);
			break;
		case 26:
			set("010111111010" + "101111111101"
					+ "011111111110" + "111111111111" + "011111111110"
					+ "101111111101" + "010111111010",7,12, 200000,  26,4);
			break;
		case 27:
			set("111111111111" + "101001100101"
					+ "111101101111" + "101001100101" + "111101101111"
					+ "101001100101" + "111111111111",7,12, 200000, 26,4);
			break;
		case 28:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111",7,12, 240000,  30,1);
			break;
		case 29:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111",7,12, 240000,  32,2);
		case 30:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111",7,12, 240000,  34,3);
			break;
		case 31:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111011110111"
					+ "110001100011" + "100001100001",7,12, 200000,  30,4);
			break;
		case 32:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111",7,12, 200000,  30,4);
			break;
		case 33:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111",7,12, 200000,  34,0);
			break;
		case 34:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111",7,12, 180000,  40,1);
			break;
		case 35:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111",7,12, 120000,  40,2);
			break;
		case 36:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111",7,12, 180000,  40,3);
			break;
		case 37:
			set("110011110011" + "100111111001"
					+ "001101101100" + "101111111101" + "101011110101"
					+ "001101101100" + "100110011001" + "011011110110",8,12, 200000,  30,4);
			break;
		case 38:
			set("010011110010" + "111111111111"
					+ "010111111010" + "111111111111" + "111111111111"
					+ "010111111010" + "111111111111" + "010011110010",8,12, 200000,  32,4);
			break;
		case 39:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111",8,12, 200000,  40,4);
			break;
		case 40:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111",8,12, 180000,  40,0);
			break;
		case 41:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111", 8,12,180000,  42,1);
			break;
		case 42:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111",8,12, 180000,  42,2);
			break;
		case 43:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111",8,12, 140000, 44,3);
			break;
		case 44:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111",8,12, 140000,  44,4);
			break;
		case 45:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111",8,12, 120000,  46,0);
			break;
		case 46:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111",8,12, 120000,  46,1);
			break;
		case 47:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111",8,12, 90000,  48,2);
			break;
		case 48:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111",8,12, 90000,  48,3);
			break;
		default:
			set("111111111111" + "111111111111"
					+ "111111111111" + "111111111111" + "111111111111"
					+ "111111111111" + "111111111111",7,12, 240000,  25,4);
			break;
		}
	}	
	
	public String getMap(String s, int row, int col, int deltaRow, int deltaCol){
		int[][] map = new int[row][col];
		int [][] mapTar = new int[row-deltaRow][col-deltaCol];
		
		int r,c;			
		r=0;
		c=0;	
			
		for(int i=0;i<s.length();i++){
			if(s.substring(i, i+1).equals("1")){							
				map[r][c] = 1;
			}
			else{
				map[r][c] = -1;
			}
			c++;
			if(c==col){
				c=0;
				if(r<row-1)
					r++;
				else
					break;
			}			
		}
		
		String mapStr="";
		int total=0;
		boolean isUpRow, isUpCol;
		int rowDevide = row/2-1, colDevide = col/2-1;
		for(int i=0;i<row-deltaRow;i++){
			for(int j=0; j<col-deltaCol;j++){
				if(i>=rowDevide){
					isUpRow = true;
				}
				else
					isUpRow = false;
				if(j>=colDevide){
					isUpCol = true;							
				}
				else 
					isUpCol = false;
				mapTar[i][j] = map[isUpRow?i+deltaRow:i][isUpCol?deltaCol+j:j];				
				if(mapTar[i][j]!=-1){
					total++;
					mapStr+="1";
				}
				else{			
					mapStr+="0";
				}				
			}
		}			
		if(total%2!=0){
			mapStr.replaceFirst("0", "1");
		}		
		return mapStr;
	}
}